package cliSAT;

import geneticsatsolver.Problem;
import geneticsatsolver.Solver;
import geneticsatsolver.Status;

/**
 * Class CliSAT provide a command line interface for SAT problem solving
 *
 * @author igor
 */
public class CliSAT {

    public static void main(String args[]) throws InterruptedException {
        if (args.length == 1) {
            Problem problem = new Problem(args[0]);
            if (problem.done) {
                Solver solver = new Solver(problem);
                solver.start();
                boolean solved = solver.getStatus().solved;
                Status status = null;
                int lastgen = 0;
                while (!solved) {
                    status = solver.getStatus();
                    if (status.generation > lastgen) {
                        solved = status.solved;
                        System.out.println("Generation: " + status.generation + "Max Fitness: " + status.maxFitness + " Mean fitness: " + status.meanFitness + " Min fitness: " + status.minFitness);
                        lastgen = status.generation;
                    }
                    Thread.sleep(100);
                }
                if (solved) {
                    boolean[] ch = status.thebest.getChromosome();
                    String solution = "";
                    for (int k = 0; k < ch.length; k++) {
                        if (ch[k]) {
                            solution += Integer.toString(k + 1) + " ";
                        } else {
                            solution += Integer.toString(-1 * (k + 1)) + " ";
                        }
                    }
                    System.out.println(solution);
                }
            } else {
                System.err.println("Unable to load problem file!");
            }
        } else {
            System.err.println("SAT problem filename argument needed!");
        }
    }
}
